<?php

namespace services\rbac;

use Yii;
use api\modules\v1\forms\rbac\ModuleForm;
use common\helpers\CommonHelper;
use common\components\Service;
use common\models\rbac\Module;

/**
 * Class ModuleService
 * @package services\rbac
 */
class ModuleService extends Service
{
    /**
     * 根据ID查找信息
     *
     * @param int $id
     * @return Module|null
     */
    public function findById(int $id = 0): ?Module
    {
        return Module::find()->where(['id' => $id])->one();
    }

    /**
     * 查找列表
     *
     * @param array $condition
     * @return array|\yii\db\ActiveRecord[]
     */
    public function findList(array $condition = [])
    {
        $query = Module::find();

        // select
        isset($condition['select']) && $query->select($condition['select']);

        // condition
        isset($condition['id']) && $query->andWhere(['id' => $condition['id']]);
        isset($condition['status']) && $query->andWhere(['status' => $condition['status']]);

        return $query->asArray()->all();
    }

    /**
     * 模块列表
     *
     * @return array
     */
    public function getListPage(array $params = [], int $page = 0, int $pageSize = 0)
    {
        $query = Module::find()
            ->where([
                'status' => [Module::STATUS_INVALID, Module::STATUS_ENABLE]
            ]);

        $order = ['status' => SORT_DESC, 'sort' => SORT_ASC];

        return $this->getListPageRes($query, $page, $pageSize, $order);
    }

    /**
     * 格式化模块列表
     *
     * @param array $data
     * @return array
     */
    public function getListPageFormat(array $data = [])
    {
        $list = [];
        foreach ($data as $row) {
            $list[] = [
                'id' => $row['id'],
                'name' => $row['name'],
                'flag' => $row['flag'],
                'url' => $row['url'],
                'class_style' => $row['class_style'],
                'status' => $row['status'],
                'status_str' => Module::$statuss[$row['status']],
                'sort' => $row['sort'],
                'created_at_str' => $row['created_at'] ? date('Y-m-d H:i:s', $row['created_at']) : '',
            ];
        }

        return $list;
    }

    /**
     * 新增模块
     *
     * @param ModuleForm $model
     * @return array|mixed
     */
    public function create(ModuleForm $model)
    {
        try {
            // 1.新增模块
            $module = new Module();
            $module->name = $model->name;
            $module->url = $model->url;
            $module->class_style = $model->class_style;
            $module->status = $model->status;
            $module->sort = $model->sort;
            $module->saveAndCheckResult();

            return ['id' => $module->id];
        } catch (\Exception $e) {
            return CommonHelper::api(422, $e->getMessage());
        }
    }

    /**
     * 编辑模块
     *
     * @param ModuleForm $model
     * @return array|mixed
     */
    public function edit(ModuleForm $model)
    {
        try {
            // 1.编辑模块
            $module = $model->getModule();
            $module->name = $model->name;
            $module->url = $model->url;
            $module->class_style = $model->class_style;
            $module->status = $model->status;
            $module->sort = $model->sort;
            $module->saveAndCheckResult();

            return ['id' => $module->id];
        } catch (\Exception $e) {
            return CommonHelper::api(422, $e->getMessage());
        }
    }

    /**
     * 删除模块
     *
     * @param ModuleForm $model
     * @return array|mixed
     */
    public function delete(ModuleForm $model)
    {
        try {
            // 1.更新模块
            $module = $model->getModule();
            $module->status = Module::STATUS_DELETE;
            $module->saveAndCheckResult();

            return ['id' => $module->id];
        } catch (\Exception $e) {
            return CommonHelper::api(422, $e->getMessage());
        }
    }
}
